﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAProjectLibrary;
using XNAProject.Core;

namespace XNAProject
{
    public sealed class StartMenuState : BaseGameState, IStartMenuState
    {
        private Texture2D texture;
        private Texture2D background;                                                                   //porps
        private SpriteFont font;
        private int selected;

        private string[] entries = 
            {
                "Join Game",
                "Create Game",                
                "Fleet Manager",
                "Options",
                "Credits",
                "Exit Game"
            };                                                                                              //menuitems


        public StartMenuState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IStartMenuState), this);                                        //register service

        }

        public override void Update(GameTime gameTime)
        {
            if (Input.WasPressed(0, Buttons.DPadUp, Keys.Up))                                               //input handling
            {
                selected--;
            }
            if (Input.WasPressed(0, Buttons.DPadDown, Keys.Down))
            {
                selected++;
            }

            if (selected < 0)                                                                               //loop
                selected = entries.Length - 1;
            if (selected == entries.Length)
                selected = 0;

            if (Input.WasPressed(0, Buttons.Start, Keys.Enter) ||
                (Input.WasPressed(0, Buttons.A, Keys.Space)))
            {                                                                                               //handle selection
                switch (selected)
                {
                    case 0:                                                                                 //Join Game
                        {                            
                            GameManager.ChangeState(OurGame.JoinMenuState.Value);
                            break;
                        }
                    case 1:                                                                                 //Create Game
                        {
                            GameManager.ChangeState(OurGame.HostMenuState.Value);
                            break;
                        }
                    case 2:                                                                                 //FleetManager
                        {
                            OurGame.FleetManagerState = new FleetManagerState(OurGame);
                            GameManager.ChangeState(OurGame.FleetManagerState.Value);
                            break;
                        }
                    case 3:                                                                                 //options menu
                        {
                            GameManager.ChangeState(OurGame.OptionsMenuState.Value);
                            break;
                        }
                    case 4:                                                                                 //Credits
                        {
                            GameManager.ChangeState(OurGame.CreditsState.Value);
                            break;
                        }
                    case 5:                                                                                 //Exit
                        {
                            OurGame.Exit();
                            break;
                        }
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 pos = new Vector2((GraphicsDevice.Viewport.Width - texture.Width) / 2,                  //draw position
                (GraphicsDevice.Viewport.Height - texture.Height) / 2);
            Vector2 position = new Vector2(pos.X + 140, pos.Y + texture.Height / 2);

            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            OurGame.spriteBatch.Draw(background, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 4f, SpriteEffects.None, 1f);
            OurGame.spriteBatch.Draw(texture, pos, Color.White);

            for (int i = 0; i < entries.Length; i++)
            {
                Color color;
                float scale;

                if (i == selected)
                {
                    // The selected entry is yellow, and has an animating size.
                    double time = gameTime.TotalGameTime.TotalSeconds;

                    float pulsate = (float)Math.Sin(time * 12) + 1;

                    color = Color.White;
                    scale = 1 + pulsate * 0.05f;
                }
                else
                {
                    // Other entries are white.
                    color = Color.Blue;
                    scale = 1;
                }
                // Draw text, centered on the middle of each line.
                Vector2 origin = new Vector2(0, font.LineSpacing / 2);
                Vector2 shadowPosition = new Vector2(position.X - 2, position.Y - 2);

                //Draw Shadow
                OurGame.spriteBatch.DrawString(font, entries[i],
                    shadowPosition, Color.Black, 0, origin, scale, SpriteEffects.None, 0);
                //Draw Text
                OurGame.spriteBatch.DrawString(font, entries[i],
                    position, color, 0, origin, scale, SpriteEffects.None, 0);

                position.Y += font.LineSpacing;
            }
            OurGame.spriteBatch.End();                                                                      //draw the menu

            base.Draw(gameTime);
        }

        protected override void StateChanged(object sender, EventArgs e)
        {
            base.StateChanged(sender, e);

            if (GameManager.State != this.Value)                                                            //event toggle visible
                Visible = true;
        }

        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>(@"Textures\Menu\startMenu");
            background = Content.Load<Texture2D>(@"Textures\Menu\MenuBackground"); //load textire, font
            font = Content.Load<SpriteFont>(@"Fonts\menu");

            base.LoadContent();

        }
    }
}
